package com.sbcb.mongodb.service;

import com.fasterxml.jackson.databind.ObjectReader;
import com.mongodb.ExplainVerbosity;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.Indexes;
import com.sbcb.mongodb.config.MongoDBServer;
import com.sbcb.mongodb.entity.Order;
import org.bson.Document;
import org.bson.codecs.configuration.CodecProvider;
import org.bson.codecs.configuration.CodecRegistry;
import org.bson.codecs.pojo.PojoCodecProvider;
import org.bson.conversions.Bson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import static com.mongodb.MongoClientSettings.getDefaultCodecRegistry;
import static org.bson.codecs.configuration.CodecRegistries.fromProviders;
import static org.bson.codecs.configuration.CodecRegistries.fromRegistries;

@Service("nativeMongoDBService")
public class NativeMongoDBService {
    @Autowired
    MongoDBServer mongoDBServer;

    @Autowired
    MongoDatabase mongoDatabase;

    public String createCollection(){
        String collectionName = "collection-"+System.nanoTime();
        mongoDatabase.createCollection(collectionName);
        return collectionName;
    }
    public long documentAmount(){
        MongoCollection<Document> mongoCollection = mongoDatabase.getCollection(mongoDBServer.getCollection());
        return mongoCollection.estimatedDocumentCount();
    }
    public List<String> listCollections(){
        List<String> collectionList = new ArrayList<>();
        for(String name: mongoDatabase.listCollectionNames()){
            collectionList.add(name);
        }
        return collectionList;
    }

    public String createIndex(){
        MongoCollection<Document> mongoCollection = mongoDatabase.getCollection(mongoDBServer.getCollection());
        //mongoCollection.createIndex(Indexes.ascending("orderID"));

        return mongoCollection.createIndex(Indexes.ascending("orderItemList.productName"));
    }

    public List<Order> findOrderList(){
        MongoCollection<Order> mongoCollection = mongoDatabase.getCollection(mongoDBServer.getCollection(), Order.class);
        List<Order> orderList = new ArrayList<>();
        Bson filter = Filters.and(Filters.eq("orderID", "ORDER80003"));
        mongoCollection.find(filter).into(orderList);
        return orderList;
    }

    public Document executeExplain(){
        MongoCollection<Order> mongoCollection = mongoDatabase.getCollection(mongoDBServer.getCollection(), Order.class);
        Bson filter = Filters.and(Filters.eq("orderID", "ORDER80003"));
        return mongoCollection.find(filter).explain(ExplainVerbosity.EXECUTION_STATS);
    }

    public void savePOJO(){
        CodecProvider pojoCodecProvider = PojoCodecProvider.builder().automatic(true).build();
        CodecRegistry pojoCodecRegistry = fromRegistries(getDefaultCodecRegistry(), fromProviders(pojoCodecProvider));
    }
}
